package com.leetCode.StringDemo;



import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LongestPalindromeDemo {
    public static void main(String[] args) {
        System.out.println(DateUtil.beginOfMinute(DateUtil.date()));
        System.out.println(DateUtil.endOfMinute(DateUtil.date()));
        System.out.println(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), -1)));
    }

    /**
     * 最长回文子串
     * 给你一个字符串 s，找到 s 中最长的回文子串。
     *
     * 如果字符串的反序与原始字符串相同，则该字符串称为回文字符串。
     *
     * 示例 1：
     *
     * 输入：s = "babad"
     * 输出："bab"
     * 解释："aba" 同样是符合题意的答案。
     * 示例 2：
     *
     * 输入：s = "cbbd"
     * 输出："bb"
     * 提示：
     *
     * 1 <= s.length <= 1000
     * s 仅由数字和英文字母组成
     * Related Topics
     * 双指针
     * 字符串
     * 动态规划
     */
    public static String longestPalindrome1(String str) {
        if (Objects.isNull(str) || str.length() <= 1){
            return str;
        }
        String resultStr = "";
        for (int i = 0;i < str.length();i++){
            int left = 0;
            int right = i;
            while (left <= right){
                //正串
                String startStr = str.substring(left,right + 1);
                //反串
                String endStr = IntStream.rangeClosed(0, startStr.length() - 1)
                        .mapToObj(st -> startStr.charAt(startStr.length() - st - 1))
                        .map(String::valueOf)
                        .collect(Collectors.joining());
                if (startStr.equals(endStr) && startStr.length() > resultStr.length()){
                    resultStr = startStr;
                }
                left++;
            }
        }
        return resultStr;
    }

    public static String longestPalindrome2(String str) {
        int len = str.length();
        if (len < 2){
            return str;
        }
        int maxLen = 1;
        int begin = 0;
        boolean[][] dp = new boolean[len][len];
        for (int i = 0;i < len;i++){
            dp[i][i] = true;
        }
        char[] chars = str.toCharArray();
        for (int L = 2;L <= len;L++){//子串长度
            for (int i = 0;i < len;i++){//左边界
                int j = i + L - 1;
                //如果越界直接返回
                if (j >= len){
                    break;
                }
                if (chars[i] != chars[j]){
                    dp[i][j] = false;
                } else {
                    if (j - i < 3){
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i+1][j-1];
                    }
                }
                if (dp[i][j] && j - i + 1 > maxLen){
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }
        return str.substring(begin,begin + maxLen);
    }


}
